<?php 
namespace app\authorization\model;

use think\Model;
use think\Db;
/**
 * 账户控制模型
 */
class ApiAccessAuthorityModel extends Model
{

	/**
	 *许可验证
	 *许可成功返回公司信息  许可失败 false
	 */
	public function accountPermission( $appid , $appsecret = '' , $flag = false )
	{
		$where = [
			'aid' => '==='.$appid
		];
		$company = Db::name( 'company' )->where( $where )->find();
		$key = md5($company['id'].$company['create_time']);
		$company ['key']  = $key;
		if( $flag ){
			$secret = md5(md5($company['id'].$appid.$key.$company['create_time']));
			if( $appsecret !== $secret ){
				return false;
			}
		}
		return $company;
	}
	/**
	 * 根据账号密码获取许可
	 * 许可成功  返回公司信息   许可失败返回 false
	 * @return [type] [description]
	 */
	public function accountPermissionByPwd( $userLogin , $userPwd )
	{	
		$result  = [ 'status' => false , 'message' => '' , 'code' => 0 ];
		$where = [
			'user_login' => $userLogin,
			'user_pass'	=> $userPwd
		];
		$userInfo = Db::name( 'user' )->where( $where )->find();
		if( empty( $userInfo ) ){
			return [ 'status' => false , 'message' => '验证账户信息失败!' , 'code' => 10008 ];
		}
		if( $userInfo ['user_status'] == 0 ){
			return [ 'status' => false , 'message' => '账户被禁用!' , 'code' => 10009 ];
		}
		$companyId = Db::name('user_attach')->where( 'user_id' , $userInfo ['id'] )->value( 'company_id' );
		$company = Db::name( 'company' )->where( 'id' , $companyId )->find();
		if( $company [ 'active' ] == 0 ){
			return [ 'status' => false , 'message' => '账户未被激活!' , 'code' => 10010 ];
		}
		if( $company [ 'period' ] < time() ){
			return [ 'status' => false , 'message' => '账户已过期!' , 'code' => 10011 ];
		}
		$company [ 'key' ] = md5($company['id'].$company['create_time']);

		return $company;
	}
}